//
//  JWMeViewController.m
//  jinwang
//
//  Created by jzb on 2018/2/24.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWMeViewController.h"
#import "AboutMeCell.h"
#import "GeRenZiLiaoVC.h"
#import "UserInfoModel.h"
#import "MyPurseVC.h"
#import "SettingsVC.h"
#import "JWMyCollectionListVC.h"
#import "JWMeMemberRechargeVC.h"

@interface JWMeViewController ()


@property (nonatomic,strong) UIButton *messageBtn;

@property (nonatomic,strong) AboutMeCell *cell;



@end

@implementation JWMeViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    BOOL isLogin = [[ToolManager sharedManager] checkLoginStateAtVC:self];
//    if (isLogin) {
//        [self net_getUserInfo];
//    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIBarButtonItem *messageItem = [[UIBarButtonItem alloc]initWithCustomView:self.messageBtn];
//    self.navigationItem.rightBarButtonItem = messageItem;
    
    [self setUpTableView];
    [self net_getUserInfo];
    //通知 - 登录成功后返回，刷新页面
    [kNotificationCenter addObserver:self selector:@selector(updateMineVC) name:@"k_notify_updateMeVC" object:nil];
}

//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([AboutMeCell class])]];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        BOOL isLogin = [[ToolManager sharedManager] checkLoginStateAtVC:self];
        if (isLogin) {
            [self net_getUserInfo];
        }
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[AboutMeCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.GeRenZiLiaoBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.MyWalletBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.MyCollectBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.BackCardBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.ConfigurationBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.VIPBtn addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.cell.HeaderImg sd_setImageWithURL:[NSURL URLWithString:k_user_headsmall] placeholderImage:[UIImage imageNamed:@"jw_img_404"]];
    
    if (kStringIsEmpty(k_user_phone)) {
        self.cell.User_PhoneLabel.text = @"账号：";
    }else{
        self.cell.User_PhoneLabel.text = [NSString stringWithFormat:@"账号：%@",k_user_phone];
    }
    
    if (kStringIsEmpty(k_user_nickname)) {
        self.cell.User_NicknameLabel.text = @"昵称：";
    }else{
        self.cell.User_NicknameLabel.text = [NSString stringWithFormat:@"昵称：%@",k_user_nickname];
    }
//    [self.cell.zhaocheBut addTarget:self action:@selector(cellButtonOnclikc:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 360;
}
- (void)cellButtonOnclikc:(UIButton *)sender{
    
    if (sender.tag == 1000) {
        
        GeRenZiLiaoVC *VC = [[GeRenZiLiaoVC alloc]init];

        [self.navigationController pushViewController:VC animated:YES];       
        
    }else if (sender.tag == 1001){
        MyPurseVC *VC = [[MyPurseVC alloc]init];
                
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1002){
        
        JWMyCollectionListVC *VC = [[JWMyCollectionListVC alloc]init];
        
        VC.title = @"我的收藏";
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1003){
        
        
    }else if (sender.tag == 1004){
        
        SettingsVC *VC = [[SettingsVC alloc]init];
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1006){
        //会员冲值
        JWMeMemberRechargeVC *VC = [[JWMeMemberRechargeVC alloc]init];
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }
}
#pragma mark -懒加载
- (UIButton *)messageBtn{
    if (!_messageBtn) {
        _messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _messageBtn.frame = CGRectMake(0, 0, 44, 44);
        [_messageBtn setImage:[UIImage imageNamed:@"jw_aboutMe_message"] forState:UIControlStateNormal];
        [_messageBtn addTarget:self action:@selector(messageButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _messageBtn;
}
- (void)messageButOnclick:(UIButton *)sender{
    
    
}

#pragma mark -网络请求
//获取用户信息
-(void)net_getUserInfo{
    [[DDNetworkManagerDate makeUrl_getuserinfo_with_token:k_user_token] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        
        
        NSLog(@"----->>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            NSDictionary *data = [responseObject objectForKey:kRequsetDataKey];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_id"]] forKey:@"user_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_nickname"]] forKey:@"user_nickname"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_portrait"]] forKey:@"user_headsmall"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_money"]] forKey:@"user_money"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_truename"]] forKey:@"user_truename"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_birthday"]] forKey:@"user_birthday"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sex"]] forKey:@"user_sex"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"media_id"]] forKey:@"media_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"shop_id"]] forKey:@"shop_id"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sign"]] forKey:@"user_sign"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_qrcode"]] forKey:@"user_qrcode"];
            [kUserDefaults setObject:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_sn"]] forKey:@"user_sn"];
            
            [kUserDefaults synchronize];
        }else{
            //注销token
            [kUserDefaults removeObjectForKey:@"user_token"];
            [kUserDefaults synchronize];
        }
        
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}


#pragma mark -通知
//通知 - 登录成功后返回，刷新页面
-(void)updateMineVC{
    //    [self net_getNewMessageNum];
    [self net_getUserInfo];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
